<?php 
	// 验证用户是否登录
	include 'fckeditor/fckeditor.php';
	require_once 'session-check.php';
	require_once '../util/dbconnection.php';;
	require_once '../util/public.php';
	require_once '../util/error.php';
	
	$user = $_SESSION['login_user'];
	$user_id = $_SESSION['login_id'];
	
	// 处理文章添加和修改
	$comact = $_GET['action'];
	if( $comact != 'add' && $comact != 'edit' ){
		$p_title = isset( $_POST['title'] ) ? trim($_POST['title']) : '';
		$p_cat = isset( $_POST['category'] ) ? trim($_POST['category']) : '';
		if( get_magic_quotes_gpc() ){
			$p_con = stripslashes( $_POST['myFCKeditor'] );
		}else{
			$p_con = $_POST['myFCKeditor'];
		}
		$p_date = date("Y-m-d H:i:s",mktime());
		
		if( $p_title == '' ){
			site_die("请填写标题");
		}else if( $p_con == '' ){
			site_die("请填写内容");
		}
		
		if( $comact == 'commit-add' ){
			if( $newid = add_post( $user_id, $p_title, $p_con, $p_cat, $p_date ) == FALSE ){
				site_die("保存文章失败，请重试");
			}else{
				if( $p_cat == "about" ){
					$re_url = "../intro.php?id=".$newid;
				}else if( $p_cat == "knowledge" ){
					$re_url = "../knowledge.php?id=".$newid;
				}
				site_redirct($re_url);
			}
		}else if( $comact == 'commit-edit' ){
			update_post($_GET['id'], $p_title, $p_con, $p_cat, $p_date );
			if( $p_cat == "about" ){
					$re_url = "../intro.php?id=".$_GET['id'];
				}else if( $p_cat == "knowledge" ){
					$re_url = "../knowledge.php?id=".$_GET['id'];
				}
			site_redirct($re_url);
		}
	}
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>站点管理-管理文章</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link rel="stylesheet" type="text/css" href="../style/admin.css"/>
	</head>
	<body>
	<div class="head">
		站点后台管理  [<a href=".." style="font-color:white">返回站点</a>]
	</div>
	<div class="userinfo">
		当前登录用户:
		<?php 
			echo $user;
			echo ' [<a href="user-action.php?action=logout">注销登陆</a>]';
			echo ' [<a href="user-admin.php">修改密码</a>]';
		?>
	</div>
	<div class="container">
		<div class="left">
			<h1>功能列表</h1>
			<ul>	
				<li><a href="user-admin.php">管理用户</a></li>
				<li><a href="post-admin.php">管理文章</a><ul><li>添加新文章</li></ul></li>
				<li><a href="feedback-admin.php">管理评论</a></li>
			</ul>
		</div>
		<div class="right">
		<?php
			if( $_GET['action'] == "add" ){
				echo "<h1>文章管理--添加新文章</h1>";
			}else if( $_GET['action'] == "edit" ){
				echo "<h1>文章管理--编辑文章</h1>";
				
				// 数据库取数据
				$post = get_post_by_id( $_GET['id'] );
			}
			echo '<div id="editarea">';
			$act_url = "commit-".$_GET['action']."&id=".$_GET['id'];
			echo '<form action="post-add-edit.php?action='.$act_url.'" method="post" >';
			echo "<ul>";
			echo '<li>文章标题: <input type="text" name="title" id="title" value="'.$post['title'].'" size="50" /></li>';
			echo '<li>文章分类: <select id="category" name="category">';
			echo '<option value="about"';
			if ( $post['category'] == "about"  )
				echo " selected ";
			echo '>公司介绍</option>';
			echo '<option value="knowledge"';
			if ( $post['category'] == "knowledge"  )
				echo " selected ";
			echo '>花卉知识</option>';
			echo '</select></li>';
			echo '<li>编辑内容: </li>';
			echo "</ul>";
			$sBasePath = $_SERVER['PHP_SELF'] ;
			$sBasePath = substr($sBasePath, 0, strpos( $sBasePath, "post-add" ));
			$myFCKeditor = new FCKeditor('myFCKeditor');
			$myFCKeditor->Config['SkinPath'] = $sBasePath.'fckeditor/editor/skins/office2003/';
			$myFCKeditor->BasePath	= $sBasePath."fckeditor/";
			if( $_GET['action'] == "edit" )
				$myFCKeditor->Value= $post['content'];
			$myFCKeditor->Create();
			echo '<p><input type="submit" id="submit" name="submit" value="发表"></p>';
			echo '</form>';
			echo '</div>';
		?>
		</div>
	</div>	
	</body>
</html>
